package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.service.CourseBaseInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseBsseInfoServiceImpl implements CourseBaseInfoService {
    @Autowired
    private CourseBaseMapper courseBaseMapper ;

    @Autowired
    private CourseMarketMapper courseMarketMapper ;

    @Autowired
    private CourseCategoryMapper courseCategoryMapper ;

    @Override
    public PageResult<CourseBase> queryCourseBaseList(Long companyId,PageParams pageParams, QueryCourseParamsDto queryCourseParamsDto) {

        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(queryCourseParamsDto.getCourseName()),CourseBase::getName,queryCourseParamsDto.getCourseName());
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamsDto.getAuditStatus()),CourseBase::getAuditStatus,queryCourseParamsDto.getAuditStatus());
        queryWrapper.eq(StringUtils.isNotEmpty(queryCourseParamsDto.getPublishStatus()),CourseBase::getStatus,queryCourseParamsDto.getPublishStatus());
        queryWrapper.eq(CourseBase::getCompanyId,companyId);

        Page<CourseBase> page = new Page<>(pageParams.getPageNo(),pageParams.getPageSize()) ;

        Page<CourseBase> pageResult = courseBaseMapper.selectPage(page,queryWrapper);
        List<CourseBase> records = pageResult.getRecords();
        long total = pageResult.getTotal();

        PageResult<CourseBase> result = new PageResult<CourseBase>(records,total,pageParams.getPageNo(),pageParams.getPageSize());
        return result;
    }

    @Override
    @Transactional
    public CourseBaseInfoDto createCourseBase(Long companyId, AddCourseDto courseDto) {
        //合法性校验
        if (StringUtils.isBlank(courseDto.getName())) {
            throw new RuntimeException("课程名称为空");
        }

        if (StringUtils.isBlank(courseDto.getMt())) {
            throw new RuntimeException("课程分类为空");
        }

        if (StringUtils.isBlank(courseDto.getSt())) {
            throw new RuntimeException("课程分类为空");
        }

        if (StringUtils.isBlank(courseDto.getGrade())) {
            throw new RuntimeException("课程等级为空");
        }

        if (StringUtils.isBlank(courseDto.getTeachmode())) {
            throw new RuntimeException("教育模式为空");
        }

        if (StringUtils.isBlank(courseDto.getUsers())) {
            throw new RuntimeException("适应人群为空");
        }

        if (StringUtils.isBlank(courseDto.getCharge())) {
            throw new RuntimeException("收费规则为空");
        }



        CourseBase courseBase = new CourseBase();
        BeanUtils.copyProperties(courseDto,courseBase);
        courseBase.setCompanyId(companyId);

        courseBase.setCreateDate(LocalDateTime.now());
        courseBase.setAuditStatus("202002");
        courseBase.setStatus("203001");


        int insert = courseBaseMapper.insert(courseBase);
        //获取课程id
        Long id = courseBase.getId();

        CourseMarket courseMarket = new CourseMarket();

        BeanUtils.copyProperties(courseDto,courseMarket);
        courseMarket.setId(id);

        String charge = courseDto.getCharge();
        if(charge.equals("201001")){
            if(courseMarket.getPrice() == null || courseMarket.getPrice()<=0){
//                throw new RuntimeException("添加课程失败");
                XueChengPlusException.cast("课程收费价格不能为空，且价格必须大于0");
            }

        }
        int insert1 = courseMarketMapper.insert(courseMarket);

        if(insert<=0 || insert1<=0){
            throw new RuntimeException("添加课程失败");
        }

        CourseBaseInfoDto courseBaseInfo = getCourseBaseInfo(id);

        return courseBaseInfo;
    }

    @Override
    @Transactional
    public CourseBaseInfoDto UpdateCourseBase(Long companyId, EditCourseDto editCourseDto) {
        Long courseId = editCourseDto.getId();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(courseBase==null){
            XueChengPlusException.cast("课程不存在");
        }
        if(!companyId.equals(courseBase.getCompanyId())){
            XueChengPlusException.cast("本机构只能修改本机构课程");
        }

        BeanUtils.copyProperties(editCourseDto,courseBase);
        courseBase.setChangeDate(LocalDateTime.now());//修改时间

        //查询营销信息
        String charge = editCourseDto.getCharge();
        if(charge.equals("201001")){
            if(editCourseDto.getPrice() == null || editCourseDto.getPrice()<=0){
//                throw new RuntimeException("添加课程失败");
                XueChengPlusException.cast("课程收费价格不能为空，且价格必须大于0");
            }

        }
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
        BeanUtils.copyProperties(editCourseDto,courseMarket);
        int insert1 = courseMarketMapper.updateById(courseMarket);



        int i = courseBaseMapper.updateById(courseBase);
        if(i<=0 || insert1<=0){
            XueChengPlusException.cast("修改失败");
        }

        CourseBaseInfoDto courseBaseInfo = getCourseBaseInfo(courseId);
        return courseBaseInfo;
    }

    @Override
    public CourseBaseInfoDto getCourseBaseInfo(Long courseId){
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);
        BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);

        String mt = courseBase.getMt();
        String st = courseBase.getSt();

        CourseCategory mtCategory = courseCategoryMapper.selectById(mt);
        CourseCategory stCategory = courseCategoryMapper.selectById(st);
        if(mtCategory!=null){
            courseBaseInfoDto.setMtName(mtCategory.getName());
        }
        if(stCategory!=null){
            courseBaseInfoDto.setStName(stCategory.getName());
        }

        return courseBaseInfoDto;
    }
}
